﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Data;

namespace PJ.Business
{
    /// <summary>
    /// EntityFragment class
    /// </summary>
    /// <typeparam name="OD">Inner data object</typeparam>
    public abstract class EntityFragment<OD> : BusinessObject<OD>, IEntityFragment
        where OD : IEntityFragmentData
    {
//        private IEntity _Owner;

        public EntityFragment(OD data)
            : base(data)
        {
        }
        public EntityFragment()
        {
        }

        public string OwnerId
        {
            get
            {
                return ObjectData.OwnerId;
            }
            set
            {
                if (ObjectData.OwnerId != value)
                {
                    ObjectData.OwnerId = value;
                    //if (value == null)
                    //    _Owner = null;
                    //else
                    //    if (_Owner != null)
                    //        if (_Owner.Id != OwnerId)
                    //            Parent = ParentService.FindById(OwnerId);
                }
            }
        }

        public override void CopyFrom(IBusinessObject data)
        {
            base.CopyFrom(data);
            IEntityFragment idata = data as IEntityFragment;
            if (idata != null)
                this.OwnerId = idata.OwnerId;
        }

        //protected abstract IEntityService ParentService { get; }


        //public IEntity Parent 
        //{
        //    get
        //    {
        //        if (_Owner == null && !string.IsNullOrEmpty(OwnerId))
        //            Parent = ParentService.FindById(OwnerId);
        //        return _Owner;                
        //    }
        //    set
        //    {
        //        if (value != _Owner)
        //        {
        //            _Owner = value;
        //            if (value == null)
        //                OwnerId = null;
        //            else
        //                OwnerId = value.GlobalId;
        //        }
        //    }
        //}

    }

}

